package com.wires.lu.practice;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * 虚引用 的get方法返回总是null  形同虚设
 */
public class PhantomRefefenceDemo {
    public static void main(String[] args) throws InterruptedException {
        Object obj=new Object();
        ReferenceQueue<Object> referenceQueue=new ReferenceQueue<>();
        PhantomReference<Object> weakReference=new PhantomReference<>(obj,referenceQueue);
        System.out.println(obj);
        System.out.println(weakReference.get());
        System.out.println(referenceQueue.poll());
        System.out.println("===================");

        Thread.sleep(2000);
        obj=null;
        System.gc();

        System.out.println(obj);
        System.out.println(weakReference.get());
        System.out.println(referenceQueue.poll());
    }

}
